//
//  ECUIVC_CommentList.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/17.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUIVC_CommentList.h"
#import "HWHttpTool.h"
#import "CYUITVDSD_base.h"
#import "CYUITVDSDBridge.h"
#import "ECUITVCell_DetailMaterial.h"
#import "CourseModel.h"
#import "ECCourseCommentModel.h"
#import "ECUITVCell_CourseComment.h"
#import "ECUIView_HomeListNoData.h"
extern CGFloat TABBAR_FOOTER_HEIGHT;

@interface ECUIVC_CommentList ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger start;// 起始请求 index
}
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)ECUIView_HomeListNoData *nodataView;

@end

@implementation ECUIVC_CommentList

@synthesize tableView = _tableView;
- (void)viewDidLoad {
    [super viewDidLoad];
    start = 0;
    // Do any additional setup after loading the view.
    [self requestCommentList];
}
- (ECUIView_HomeListNoData *)nodataView
{
    if (!_nodataView) {
        _nodataView = [[ECUIView_HomeListNoData alloc]initWithFrame:CGRectMake(0, TABLEVIEWCONTENT_OFFSET_Y, kScreenWidth, 200)];
        _nodataView.imageView.image = [UIImage imageNamed:@"image_list_nodata"];
        _nodataView.textLabel.text = @"暂无相关评论";
    }
    return _nodataView;
}
-(void)addSubviews
{
    [super addSubviews];
    [self.view addSubview:self.tableView];
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, TABLEVIEWCONTENT_OFFSET_Y)];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, TABLEVIEWCONTENT_OFFSET_Y)];

    _tableView.tableHeaderView = headerView;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - self.tableView.height + TABLEVIEWCONTENT_OFFSET_Y)];
    WeakSelf(weakSelf);
    MJRefreshStateHeader *goods_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [weakSelf refreshHeader];
    }];
    goods_header.ignoredScrollViewContentInsetTop = -(TABLEVIEWCONTENT_OFFSET_Y - 10);
    self.tableView.mj_header = goods_header;
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreFooter];
    }];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;

}
- (void)showNodataView:(BOOL)flag {
    if (flag) {
        [self.tableView addSubview:self.nodataView];
    } else {
        [self.nodataView removeFromSuperview];
        
    }
}
-(void)refreshHeader{
    start = 0;
    [self requestCommentList];
}
- (void)loadMoreFooter{
    start = self.dataSource.count;
    [self requestCommentList];
}
-(void)requestCommentList{
    CourseModel *mo = self.object;
    WeakSelf(weakSelf);
    [HWHttpTool get:URL_CourseCommentListWithId(mo.modelId) params:@{@"start":@(start)} success:^(id json) {
        [weakSelf reloadTableViewData:json];
    } failure:^(NSError *error) {
        
    }];
}
-(void)reloadTableViewData:(id)json{

    id data = [json valueForKey:@"data"];
    id list = [data valueForKey:@"resources"];
    NSInteger total = [[data valueForKey:@"total"] integerValue];
    if(start == 0){
        [self.dataSource removeAllObjects];
    }
    for (id obj in list) {
        ECCourseCommentModel *mo = [[ECCourseCommentModel alloc]initWithCommentObject:obj];
        [self.dataSource addObject:mo];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (total <= self.dataSource.count) {
            self.tableView.mj_footer.hidden = YES;
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            self.tableView.mj_footer.hidden = NO;

            [self.tableView.mj_footer endRefreshing];
        }
        [self showNodataView:(self.dataSource.count <= 0)];

        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        if (self.tableView.contentSize.height <= self.tableView.height) {
            self.tableView.contentSize = CGSizeMake(self.tableView.width, self.tableView.height + TABLEVIEWCONTENT_OFFSET_Y);
            
        }
    });
    
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        //-TABBAR_TITLE_HEIGHT - kNavHeight -
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,  self.view.height) style:UITableViewStylePlain];
        [_tableView registerClass:[ECUITVCell_CourseComment class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return _tableView;
}
- (void)tableViewDidScroll:(tableViewDidScrollBlock)scrollBlock {
    self.scrollBlock = scrollBlock;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.scrollBlock(scrollView.contentOffset.y);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ECUITVCell_CourseComment *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.object = [_dataSource objectAtIndex:indexPath.row];
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        cell.separatorInset = UIEdgeInsetsMake(0, 40, 0, 10);
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        cell.layoutMargins = UIEdgeInsetsZero;
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        cell.preservesSuperviewLayoutMargins = false;
        
    }
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
